$(function () {
    $('#updateNameAndIntroduction').click(function () {
        $(this).attr('disabled', true);
        let userName = $('#userName').val();
        let userIntroduction = $('#userIntroduction').val();
        if (isEmpty(userName) || !validName(userName)) {
            toastr.warning('昵称格式错误！', '警告', {timeOut: 5000});
            $(this).attr('disabled', false);
            return;
        }
        if (isEmpty(userIntroduction) || !validLoginId(userIntroduction)) {
            toastr.warning('手机号格式错误！', '警告', {timeOut: 5000});
            $(this).attr('disabled', false);
            return;
        }
        $.ajax({
            type: 'PUT',
            url: '/profile/updateNameAndIntroduction',
            data: {userName: userName, userIntroduction: userIntroduction},
            success: function (result) {
                if (result.statusCode === 200) {
                    alert("修改成功！");
                    window.location.reload();
                } else {
                    toastr.error(result.message, {timeOut: 3000});
                }
            },
            complete: function (){
                $('#updateNameAndIntroduction').attr('disabled', false);
            }
        });
    });

    $('#updatePassword').click(function () {
        $(this).attr('disabled', true);
        let oldPassword = $('#oldPassword').val();
        let newPassword = $('#newPassword').val();
        if (isEmpty(oldPassword) || !validPassword(oldPassword)) {
            toastr.warning('密码格式错误！', '警告', {timeOut: 5000});
            $(this).attr('disabled', false);
            return;
        }
        if (isEmpty(newPassword) || !validPassword(newPassword)) {
            toastr.warning('密码格式错误！', '警告', {timeOut: 5000});
            $(this).attr('disabled', false);
            return;
        }
        $.ajax({
            type: 'PUT',
            url: '/profile/updatePassword',
            data: {oldPassword: oldPassword, newPassword: newPassword},
            success: function (result) {
                if (result.statusCode === 200) {
                    alert("修改成功！");
                    window.location.href = '/login';
                } else {
                    toastr.error(result.message, {timeOut: 3000});
                }
            },
            complete: function (){
                $('#updatePassword').attr('disabled', false);
            }
        });
    });

    // 图片点击打开文件选择框
    $('#openImg').click(function () {
        $('#profile_img').click();
    });

    // 选择头像
    $('#profile_img').on('change', function (e) {
        var image = e.target.files[0]; // 又卡了半天
        if(!checkImage(image)){
            return;
        }
        // 选择了图片
        var data = new FormData();
        data.append("Image", image);
        $.ajax({
            type: 'POST',
            processData: false,  // 注意：让jQuery不要处理数据
            contentType: false,  // 注意：让jQuery不要设置contentType
            url: '/profile/img',
            data: data,
            success: function (result) {
                if (result.statusCode === 200) {
                    window.location.reload();
                } else {
                    toastr.error(result.message, {timeOut: 3000});
                }
            }
        });
    });
});